package cn.e4j.modular.crm.service.impl;

import cn.e4j.core.dto.Dtos;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.core.shiro.ShiroUser;
import cn.e4j.core.util.AppCxt;
import cn.e4j.core.util.DateUtils;
import cn.e4j.core.util.LangUtils;
import cn.e4j.modular.crm.entity.GwfcT;
import cn.e4j.modular.crm.entity.GwfdT;
import cn.e4j.modular.crm.mapper.GwfcTMapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 费用预提表 服务实现类
 * </p>
 *
 * @author shixc
 * @since 2019-10-24
 */
@Service
public class GwfcTServiceImpl extends ServiceImpl<GwfcTMapper, GwfcT> {

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getList(Page<Object> page, Dto dto) {

        List<Map<String, Object>> gwfcDtos = this.baseMapper.listPage(page, dto);

        return gwfcDtos;
    }

    @Transactional
    public Dto addData(List<GwfcT> gwfcTList) {
        Dto outDto = Dtos.newDto();
        outDto.setAppCode(200);
        ShiroUser user = ShiroKit.getUser();
        //增加新的明细
        for (GwfcT gwfc : gwfcTList) {
            gwfc.setGWFCENT(user.getDBC());
            gwfc.insert();
        }
        return outDto;
    }

    @Transactional
    public void editData(List<GwfcT> gwfcTList) {
        ShiroUser shiroUser = ShiroKit.getUser();
        GwfcT gwfc = new GwfcT();
        UpdateWrapper gwfcUpdateWrapper = new UpdateWrapper();
        gwfcUpdateWrapper.eq("GWFCENT", shiroUser.getDBC());
        gwfcUpdateWrapper.eq("GWFCDOCNO", gwfcTList.get(0).getGWFCDOCNO());
        gwfc.delete(gwfcUpdateWrapper);
        //增加新的明细
        for (int i = 0; i < gwfcTList.size(); i++) {
            GwfcT gwfcT = gwfcTList.get(i);
            gwfcT.setGWFCMODU(shiroUser.getID());
            gwfcT.insert();
        }
    }

    @Transactional
    public void delete(GwfcT gwfcT) {
        UpdateWrapper<GwfcT> gwfcTUpdateWrapper = new UpdateWrapper<>();
        gwfcTUpdateWrapper.eq("GWFCENT", gwfcT.getGWFCENT());
        gwfcTUpdateWrapper.eq("GWFCDOCNO", gwfcT.getGWFCDOCNO());
        gwfcT.delete(gwfcTUpdateWrapper);
    }

    @Transactional
    public void delDataBatch(List<GwfcT> gwfcTList) {
        for (GwfcT gwfcT : gwfcTList) {
            UpdateWrapper<GwfcT> gwfcTUpdateWrapper = new UpdateWrapper<>();
            gwfcTUpdateWrapper.eq("GWFCENT", gwfcT.getGWFCENT());
            gwfcTUpdateWrapper.eq("GWFCDOCNO", gwfcT.getGWFCDOCNO());
            gwfcT.delete(gwfcTUpdateWrapper);
        }
    }

    @Transactional
    public void updateStatus(List<GwfcT> gwfcTList) {
        for (int i = 0; i < gwfcTList.size(); i++) {
            GwfcT gwfcT = gwfcTList.get(i);
            UpdateWrapper<GwfcT> gwfcTUpdateWrapper = new UpdateWrapper<>();
            gwfcTUpdateWrapper.eq("GWFCENT", gwfcT.getGWFCENT());
            gwfcTUpdateWrapper.eq("GWFCDOCNO", gwfcT.getGWFCDOCNO());
            gwfcT.update(gwfcTUpdateWrapper);
        }
    }

    @Transactional(readOnly = true)
    public List<Map<String, Object>> listGwfc(Dto dto) {
        List<Map<String, Object>> gwfcDtos = this.baseMapper.listGwfc(dto);
        return gwfcDtos;
    }

    @Transactional(readOnly = true)
    public Dto getCount(Dto inDto) {
        Dto count = this.baseMapper.getCount(inDto);
        return count;
    }

    @Transactional(readOnly = true)
    public List<Dto> getGwbsData(Dto dto) {
        List<Dto> gwbsDtos = this.baseMapper.getGwbsData(dto);
        return gwbsDtos;
    }

    @Transactional(readOnly = true)
    public List<Dto> getGwfaData(Dto dto) {
        List<Dto> gwfaDtos = this.baseMapper.getGwfaData(dto);
        return gwfaDtos;
    }

    @Transactional(readOnly = true)
    public Dto getRate(Dto inDto) {
        Dto rate = this.baseMapper.getGwek005(inDto);
        if (rate == null) {
            rate = Dtos.newDto();
            rate.put("RATE",1);
        }
        return  rate;
    }

    @Transactional
    public Dto autoGeneration(Dto inDto) {
        ShiroUser user = ShiroKit.getUser();
        Dto outDto = Dtos.newDto();
        outDto.put("message", "批次生成成功");
        int amount = Integer.parseInt(AppCxt.getParam("amount_digit_"));
        //根据年度、月份和区域（不选就是全部区域）从销售预测表抓取已确认的数据，按年度、月份、区域、客户、品类、业务组织统计出金额
        if (!"".equals(inDto.getString("GWFC003"))) {
            String[] data = inDto.getString("GWFC003").split(",");
            List<Dto> GWFC003List = new ArrayList<>();
            for (String GWFC003 : data) {
                Dto gwfc = Dtos.newDto();
                gwfc.put("GWFC003", GWFC003);
                GWFC003List.add(gwfc);
            }
            inDto.put("GWFC003List", GWFC003List);
        }
        List<Dto> gwbsList = this.baseMapper.getGwbsData(inDto);
        List<Dto> gwfaList = this.baseMapper.getGwfaData(inDto);
        String GWFCDOCNO = "FYYT" + DateUtils.getDate("yyyyMMddHHmmssSSS");
        int count = 1;
        if (gwbsList.size() == 0) {
            outDto.put("message", "销售预测表无数据，覆盖不成功！");
            return outDto;
        } else {
            GwfcT gwfc = new GwfcT();
            UpdateWrapper<GwfcT> gwfcTUpdateWrapper = new UpdateWrapper<>();
            gwfcTUpdateWrapper.eq("GWFCENT", user.getDBC());
            gwfcTUpdateWrapper.eq("GWFC001", inDto.getString("GWFC001"));
            gwfcTUpdateWrapper.eq("GWFC002", inDto.getString("GWFC002"));
            gwfc.delete(gwfcTUpdateWrapper);
        }
        for (int i = 0; i < gwbsList.size(); i++) {
            Dto gwbs = gwbsList.get(i);
            for (int j = 0; j < gwfaList.size(); j++) {
                Dto gwfa = gwfaList.get(j);
                BigDecimal GWFC106 = gwbs.getBigDecimal("GWBS007").multiply(gwfa.getBigDecimal("GWFA004")).setScale(amount, RoundingMode.HALF_UP); //区域
                BigDecimal GWFC107 = gwbs.getBigDecimal("GWBS007").multiply(gwfa.getBigDecimal("GWFA005")).setScale(amount, RoundingMode.HALF_UP); //总部
                if (GWFC106.compareTo(new BigDecimal(0)) == 1 && GWFC107.compareTo(new BigDecimal(0)) == 1) {
                    inDto.put("GWAK001", gwbs.getString("GWBS002"));
                    Dto gwak = this.baseMapper.getGwak(inDto);
                    Dto qDto = Dtos.newDto();
                    qDto.put("ENT", user.getDBC());
                    qDto.put("GWEK001", gwak.getString("GWAK008"));
                    qDto.put("GWEK002", inDto.getString("GWFC001") + inDto.getString("GWFC002"));
                    Dto gwek = this.getRate(qDto); //汇率
                    GwfcT gwfcT = new GwfcT();
                    gwfcT.setGWFCENT(user.getDBC());
                    gwfcT.setGWFCDOCNO(GWFCDOCNO);
                    gwfcT.setGWFCSEQ(BigDecimal.valueOf(count));
                    gwfcT.setGWFC001(gwbs.getString("GWBS012"));//年度
                    gwfcT.setGWFC002(gwbs.getString("GWBS013"));//月份
                    gwfcT.setGWFC003(gwbs.getString("GWBS016") != null ? gwbs.getString("GWBS016") : "");//区域
                    gwfcT.setGWFC004(gwbs.getString("GWBS002"));//客户
                    gwfcT.setGWFC005(gwbs.getString("GWBS017"));//品类
                    gwfcT.setGWFC006(gwbs.getString("GWBSORIG"));//业务组织
                    gwfcT.setGWFC007(gwfa.getString("GWFA001"));//费用编号
                    gwfcT.setGWFC100(gwak.getString("GWAK008"));//币别
                    gwfcT.setGWFC101(gwek.getBigDecimal("GWEK005"));//汇率
                    gwfcT.setGWFC102(gwbs.getBigDecimal("GWBS007"));//原币金额
                    gwfcT.setGWFC103(gwbs.getBigDecimal("GWBS007").multiply(gwek.getBigDecimal("RATE")).setScale(amount, RoundingMode.HALF_UP));//本币金额
                    gwfcT.setGWFC104(gwfa.getBigDecimal("GWFA004"));//区域费率
                    gwfcT.setGWFC105(gwfa.getBigDecimal("GWFA005"));//总部费率
                    gwfcT.setGWFC106(GWFC106);//区域原币
                    gwfcT.setGWFC107(GWFC107);//总部原币
                    gwfcT.setGWFC108(GWFC106.multiply(gwek.getBigDecimal("RATE")).setScale(amount, RoundingMode.HALF_UP));//区域本币
                    gwfcT.setGWFC109(GWFC107.multiply(gwek.getBigDecimal("RATE")).setScale(amount, RoundingMode.HALF_UP));//总部本币
                    gwfcT.setGWFCACTI("N"); //有效码
                    gwfcT.insert();
                    count++;
                }
            }
        }
        return outDto;
    }

}
